package com.hippomeetsskunk.savanna.types.person;

import java.util.HashMap;
import java.util.Map;

import com.hippomeetsskunk.savanna.model.ModelProvider;
import com.hippomeetsskunk.savanna.model.NourishmentModel;
import com.hippomeetsskunk.savanna.types.meta.EdibleType;
import com.hippomeetsskunk.savanna.types.person.Attributes.Attribute;
import com.hippomeetsskunk.savanna.types.resource.Edible;

public class Nourishment {

	private Map<EdibleType, Double> level = new HashMap<EdibleType, Double>();
	
	public void eat(Edible edible){
		// if eating, level starts from 0, not minus
		EdibleType type = edible.getType().getFoodType();
		
		final double before = Math.max(0.0, level.containsKey(type) ? level.get(type) : 0.0);
		final double after = Math.min(2.0, before + edible.getQuality());
		level.put(type, after);
	}
	
	public void digest(Person person){
		NourishmentModel model = ModelProvider.getInstance().getNourishmentModel();
		// calculate health state
		double deltaH = 0.0;
		for(EdibleType type : EdibleType.foodTypes()){
			final double a = model.getConsumption(person, type);
			consume(type, a);

			deltaH += level.get(type);
		}
		person.getAttributes().add(Attribute.HEALTH, deltaH);
	}
	
	private void consume(EdibleType type, double amount){
		final double r = (level.containsKey(type)) ? level.get(type) : 0.0;
		level.put(type, r - amount);
	}

	public double get(EdibleType type) {
		return (level.containsKey(type)) ? level.get(type) : 0.0; 
	}
}
